package com.dex4d.ui.qr.model;

import android.content.Context;

import com.dex4d.R;

/**
 * 作者    LiQingyi
 * 时间    2018/11/20 13:50
 * 文件    dex4d-android
 * 描述
 */
public class QRResult {
    public boolean state;
    public Error error;
    public String sign;
    public String txhash;

    public PayData paydata;

    public QRResult(String txhash) {
        this.txhash = txhash;
    }

    public QRResult(Error error) {
        this.error = error;
    }

    /**
     * 是否成功
     *
     * @return
     */
    public boolean isOk() {
        return state;
    }

    /**
     * 获取错误提示信息
     *
     * @param context
     * @return
     */
    public String getError(Context context) {
        if (error == null) error = new Error();
        return convert(context, error);
    }

   public static class Error {
        public int id;
        public String msg;

        public Error() {
            this.id = 1;
        }

        public Error(String err) {
            this.id = -1;
            this.msg = err;
        }
    }

    /**
     * 字符串转化
     *
     * @param context
     * @param error
     * @return
     */
    public String convert(Context context, Error error) {
        if (error.id > 12 || error.id < 0) {
            return error.msg == null ? context.getResources().getString(R.string.dialog_tips_error_1) : error.msg;
        } else {
            int resId = context.getResources().getIdentifier("dialog_tips_error_" + error.id, "string", context.getPackageName());
            return context.getResources().getString(resId);
        }
    }

}
